<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<%@ include file="/commons/jsp/taglibs.jsp"%>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="keywords" content="">
<meta http-equiv="Cache-Control" content="max-age=86400 public">
<meta http-equiv="X-UA-Compatible" content="IE=8">
<meta http-equiv="X-UA-Compatible" content="IE=9">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<script type="text/javascript" src="${ctx }/web/js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="${ctx }/web/js/1.js"></script>
<script type="text/javascript" src="${ctx }/web/js/2.js"></script>
<script type="text/javascript" src="${ctx}/commons/js/jquery.easyui.min.js"></script>
<!-- jqueryUI国际化 -->
<script type="text/javascript" src="${ctx}/commons/js/easyui-lang-zh_CN.js"></script>
<!-- 开发国际化的引用，主要完成后台信息在前台提示的国际化，一定要放在最后 -->
<script type="text/javascript">var ctx = '${ctx}';var actionExt = "action";var i18n = new Object();</script>
<script src="${ctx}/commons/js/i18n/message_zh_CN.js" type="text/javascript"></script>
<!-- - <link href="${ctx }/template/template_1/css/style.css" rel="Stylesheet" type="text/css" />-->
<link href="${ctx }/web/css/style.css" rel="Stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="${ctx }/web/css/basic.css">
<link rel="stylesheet" type="text/css" href="${ctx}/commons/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${ctx }/web/css/css.css">
<script type="text/javascript" src="js/z_2.js"></script>
 <link rel="stylesheet" type="text/css" href="css/shop.css">
 <style type="text/css">
 	.messager-window {
 		width: 288px ! important ;
 	}
 	.messager-body {
 		width: 266px ! important ;
 	}
 	.window-shadow{
 		width: 300px ! important ;
 	}
 	.window-header{
 		width: 288px ! important ;
 	}
 </style>
</head>

<body>
<div class="banbo clearfix">
    <div class="test" style="width: 100%; position:relative;">
        <!---头头--->
    <%@ include file="web-top.jsp"%>
<!------内容------>
<!---<div class="hanggao clearfix"></div>--->

    <div class="clearfix" style="background:#FFF;">
    	<div class="neira clearfix">
        	<div class="w1200 clearfix">
                <div class="clearfix">    	
                    <div class="page-bt clearfix">
                        <div class="pagebt-gwcmain clearfix" style="padding-left:0px;">
                            <div class="fll ts4"><span>我的购物车</span></div>
                            <div class="frr clearfix">
                                <div class="navhide">
                                    提示：<span style="color:#f00;">如缺失联系信息，工作人员将无法与您取得联系</span>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="pagebjs">
                        <div class="xmain clearfix">
                            <div class="gc2 clearfix">
                                <script type="text/javascript" src="js/shop.js"></script>
                                <div class="catbox">
                                    <table id="cartTable">
                                        <thead>
                                            <tr>
                                                <th><label><input class="check-all check" type="checkbox"/><span>&nbsp;全选</span></label></th>
                                                <th>商品</th>
                                                <th>所属店铺</th>
                                                <th>单价</th>
                                                <th>数量</th>
                                                <th>小计(元)</th>
                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                        	<c:forEach items="${myCart}" var="cart">
                                        		<tr>
	                                                <td class="checkbox"><input class="check-one check" type="checkbox" id="cb" value="${cart.goods.id}"/></td>
	                                                <td class="goods" style="width: 300px;"><a href="${ctx }/web/goodsInfo?goodsId=${cart.goods.id}" target="_blank"><img src="${ctx }/file_resource/${cart.goodsPicPath}" alt=""/><div class="shoptext"><span>${cart.goods.goodsName } </span><font>产品编号：${cart.goods.goodsCode }    产品品牌：${cart.goods.goodsBrand }</font></div></a></td>
	                                                <td width="300">${cart.shopName }</td>
	                                                <td class="price">${cart.price }</td>
	                                                <td class="count" style="text-align: center;"><!-- <span class="reduce">-</span> --><input class="count-input" type="text" value="${cart.num }"/><input type="hidden" value="${cart.goods.id }"/><!-- <span class="add">+</span> --></td>
	                                                <td class="subtotal" ><fmt:formatNumber pattern="0.00" value="${cart.totalMoney }"></fmt:formatNumber> </td>
	                                                <td class="operation" style="width: 100px;"><span class="remark" onclick="addRemark('${cart.goods.id}','${cart.remark }');">添加备注</span>&nbsp;&nbsp;&nbsp;&nbsp;<span class="delete">删除</span></td>
	                                            </tr>
                                        	</c:forEach>
                                        </tbody>
                                    </table>
                                    <div class="foot" id="foot">
                                        <div class="tianjia fl"><a href="${ctx }/web/goodsAll?typeId=${sessionScope.project.goodsTypeId }&projectId=${sessionScope.project.id }" target="_blank">继续添加其他产品</a></div>
                                        <div class="fr closing" onclick="submitCart();">提交订单</div>
                                        <div class="fr total">合计：￥<span id="priceTotal">0.00</span></div>
                                        <div class="fr selected" id="selected">已选商品<span id="selectedTotal">0</span>件</div>
                                        <div class="selected-view" style="display:none;">
                                            <div id="selectedViewList" class="clearfix" style="display:none;">
                                                <div><img src="images/1.jpg"><span>取消选择</span></div>
                                            </div>
                                            <span class="arrow" style="display:none;">◆<span>◆</span></span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <br />
            <br />
        </div>
    </div>
        <!--- 页脚 --->
        <%@ include file="web-buttom.jsp"%>  
    </div>
</div>
<form action="${ctx}/web/accountValidateCart" id="accountCartForm" method="post">
	<input type="hidden" name="goodsIds" id="goodsIds"/>
	<input type="hidden" name="noLastPriceReason" id="noLastPriceReason"/>
</form>
<form action="${ctx}/portal/index.jsp" id="toPage" method="post">
<input type="hidden" name="hrefUrl" id="hrefUrl" value="${ctx }/business/projectmanage/index"/>
</form>
<div id="addRemarkDialog"></div>
<div id="lastpricegoodsdialog"></div>
</body>
</html>
<script type="text/javascript">
		var status = '${status}';
        if(status == 'userNotLogin'){
        	$.messager.alert('提示','您长时间没有操作系统，请重新登录系统！','info',function(){top.location='${ctx}/manage';});
        } else if(status == 'notFindProject'){
        	$.messager.alert('提示','没有发现采购的项目，请到【项目管理】中选择采购项目！','info',function(){$('#hrefUrl').val("${ctx}/business/projectmanage/index");$('#toPage').submit();});
        }

     // 计算单行价格
        function getSubtotal(tr) {
            var cells = tr.cells;
            var price = cells[3]; //单价
            var subtotal = cells[5]; //小计td
            var countInput = tr.getElementsByTagName('input')[1]; //数目input
            var goodsId = tr.getElementsByTagName('input')[2].value; //商品ID
            //var span = tr.getElementsByTagName('span')[1]; //-号
            $.ajax({
    			type: "get",
    			timeout:20000,
    			url:  '${ctx}/web/setCartGoodsNum?r='+new Date().getTime(),
    			data:'goodsId='+goodsId+'&num='+countInput.value,
    			async:false,
    			success: function(data,res){
        			if(data.success){
        				price.innerHTML=data.result.price;
        				var goodsTotalPrice = data.result.price * data.result.goodsNum;
        				subtotal.innerHTML = goodsTotalPrice.toFixed(2);
        			} else if('goodsStorageNotFull' == data.result){
        				top.$.messager.alert('提示','该商品库存量不足，操作失败！','info',function(){countInput.value = 1;countInput.focus();});
        			}
    			},
    			error: function(res){
    			}
    		});
            //写入HTML
           // subtotal.innerHTML = (parseInt(countInput.value) * parseFloat(price.innerHTML)).toFixed(2);
            //如果数目只有一个，把-号去掉
           /* if (countInput.value == 1) {
                span.innerHTML = '';
            }else{
                span.innerHTML = '-';
            }*/
        }

        function deleteCartGoods(tr){
            var goodsId = tr.getElementsByTagName('input')[2].value; //商品ID
        	top.$.messager.confirm('提示','您确定要从购物车中删除该商品信息吗？',function(r){
			    if (r){
			    	 $.ajax({
			    			type: "get",
			    			timeout:20000,
			    			url:  '${ctx}/web/deleteCartGoods?r='+new Date().getTime(),
			    			data:'goodsId='+goodsId,
			    			success: function(data,res){
			    				if(data == 'userNotLogin'){
			    					top.$.messager.alert('提示','您没有登录系统，请先登录系统！','info',function(){top.location='${ctx}/manage';});
			    				}
			    				if(data == 'success'){
			    					tr.parentNode.removeChild(tr);
			    					changePrice();
			    				}
			    			},
			    			error: function(res){
			    			}
			    		});
			    }});
        }

        function changePrice(){
        	var table = document.getElementById('cartTable'); // 购物车表格
        	var tr = table.children[1].rows; //行
        	var len;
        	var price = 0;
        	var seleted = 0;
        	var check = document.getElementsByClassName('check');
        	for (var i = 0, len = tr.length; i < len; i++) {
        		price += parseFloat(tr[i].cells[5].innerHTML);
        		seleted += parseInt(tr[i].getElementsByTagName('input')[1].value);
        		check[i].checked  = true;
        		check[len].checked  = true;
    		}
    		$('#priceTotal').html(price.toFixed(2));
    		$('#selectedTotal').html(seleted);
    		
        }

        function submitCart(){
        	var selectInputs = document.getElementsByClassName('check');
        	var selGoodsIds = "";
        	for(var i = 1; i < selectInputs.length; i++ ){
            	if(i==1){
                	if(selectInputs[i].checked){
                		selGoodsIds = selectInputs[i].value;
                	}
            	} else {
            		if(selectInputs[i].checked){
                		selGoodsIds += ',' + selectInputs[i].value;
                	}
            	}
            }
            if(selGoodsIds == ""){
            	top.$.messager.alert('提示','订单中没有商品，无法提交订单！','info');
            } else {

                //提交订单操作，弹出填写非最低价采购理由
                $.ajax({
					type: "POST",
					timeout:20000,
					url:  '${ctx}/web/validatelastpricegoods',
					data: {goodsIds:selGoodsIds},
					success: function(data,res){
						if(data == true){//存在非最低价商品,提示填写理由
							top.$.messager.confirm('提示',"您存在非最低价采购商品，请填写采购理由！",function(result){
								 if(result){
									 $('#lastpricegoodsdialog').dialog({
											title: '非最低价采购理由',
											width: 450,
											height: 200,
											closed: false,
											cache: false,
											closable: false,   
											inline : true,
											modal: true,
											content: '<table class="infoTable"><tbody><tr><th align="right" width="15%">填写原因</th><td  width="36%"><textarea id="noCompetReason" name="noCompetReason" style="width:350px;" rows="6" type="text" maxlength="200" value=""></textarea></td></tr></tbody></table>',
											buttons :
													[{text:'保存',
														iconCls:'icon-ok',
														handler:function(){
														    var noCompetReason = top.$("#noCompetReason").val();
														    if(noCompetReason == null || $.trim(noCompetReason) == ''){
																top.$.messager.alert('提示',"请输入原因！",'info');
																return;
														    }
															$("#noLastPriceReason").val($("#noCompetReason").val());
															$('#lastpricegoodsdialog').dialog('close');
															//
															$('#goodsIds').val(selGoodsIds);
															$('#accountCartForm').submit();//提交表单
														}
													},{
														text:'取消',
														iconCls:'icon-cancel',
														handler:function(){
															$('#lastpricegoodsdialog').dialog('close');
															$('#goodsIds').val(selGoodsIds);
															$('#accountCartForm').submit();//提交表单
														}}]
									});
								 }else{
									$('#goodsIds').val(selGoodsIds);
									$('#accountCartForm').submit();
								 }
							 });
						} else {//不存在非最低价采购商品
							$('#goodsIds').val(selGoodsIds);
							$('#accountCartForm').submit();
						}
					}
				});
                
            	//$('#goodsIds').val(selGoodsIds);
				//$('#accountCartForm').submit();
            }
        }
        function addRemark(goodsId,remarkData){
			$('#addRemarkDialog').dialog({    
			    title:'添加商品备注',  
			    iconCls:'icon-view',
			    width: 400,    
			    height: 200,  
			    closed: false,
			    cache: false,
			    closable: false,   
			    inline : true,
			    modal: true,
			    content:"<textarea style='width: 98%;height: 94%;' id='remark'>"+remarkData+"</textarea>",
			    buttons : [{text:'提交',
			    		id:'saveEntityBt',
						//iconCls:'icon-ok',
						handler:function(){
			    			addRemarkData(goodsId);
						}
					},{
						text:'关闭',
						//iconCls:'icon-cancel',
						handler:function(){
							$('#addRemarkDialog').dialog('close');
						}}]
			});
        }

        function addRemarkData(goodsId){
            var remark = $('#remark').val();
            if(remark != null && remark != undefined && $.trim(remark) != ''){
            	$.ajax({
       			type: "post",
       			timeout:20000,
       			url:  '${ctx}/web/addCartGoodsRemark?r='+new Date().getTime(),
       			data:{'goodsId':goodsId,'remark':remark},
       			success: function(data,res){
       				if(data == 'notFindProject'){
       					$.messager.alert('提示','您可能登录时间过长，请重新登录系统！','info',function(){top.location='${ctx}/manage';});
       				} else if(data == 'notFindGoods'){
       					$.messager.alert('提示','系统未发现对应的商品信息，请正确操作系统','info');
       				} else if(data == 'success'){
       					$.messager.alert('提示','添加备注成功！','info',function(){top.location='${ctx}/web/myCart';});
       				}
       			},
       			error: function(res){
       			}
       		});
            }else {
            	top.$.messager.alert('提示','请填写备注！','info');
            }
        }

      
        
</script>